package com.kingtone.rongji.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.jakewharton.salvage.RecyclingPagerAdapter;
import com.kingtone.rongji.R;
import com.kingtone.rongji.biz.HomeImage;
import com.kingtone.rongji.tool.ListUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * ImagePagerAdapter
 * 
 * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2014-2-23
 */
public class ProductImageAdapter extends RecyclingPagerAdapter {
	private static final String TAG = "ImagePagerAdapter";
	
    private Context       context;
    private List<HomeImage> imageIdList;

    private int           size;
    private boolean       isInfiniteLoop;
    
    private DisplayImageOptions mOptions;

    public ProductImageAdapter(Context context, List<HomeImage> imageIdList) {
        this.context = context;
        this.imageIdList = imageIdList;
        this.size = ListUtils.getSize(imageIdList);
        isInfiniteLoop = false;
        mOptions = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.product_loading_img)
        		.cacheInMemory(true).cacheOnDisk(true)
				.bitmapConfig(Bitmap.Config.RGB_565).delayBeforeLoading(100).build();
    }

    public void setImgeList(List<HomeImage> imagesList)
    {
    	this.imageIdList = imagesList;
    }
    
    @Override
    public int getCount() {
        // Infinite loop
        return isInfiniteLoop ? Integer.MAX_VALUE : ListUtils.getSize(imageIdList);
    }

    /**
     * get really position
     * 
     * @param position
     * @return
     */
    private int getPosition(int position) {
		return isInfiniteLoop ? position % (size == 0 ? 1 : size) : position;
    }

    @Override
    public View getView(int position, View view, ViewGroup container) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = holder.imageView = new ImageView(context);
            holder.imageView.setScaleType(ScaleType.CENTER_INSIDE);
            view.setTag(holder);
        } else {
            holder = (ViewHolder)view.getTag();
        }
        int index = getPosition(position);
        if(imageIdList.size() > index)
        {
        	final HomeImage item = imageIdList.get(index);
            String url =  item.getImageUrl();
        	if (url != null) {
            	ImageLoader.getInstance().displayImage(url, holder.imageView, mOptions);
            }
        }
        return view;
    }

    private static class ViewHolder {

        ImageView imageView;
    }

    /**
     * @return the isInfiniteLoop
     */
    public boolean isInfiniteLoop() {
        return isInfiniteLoop;
    }

    /**
     * @param isInfiniteLoop the isInfiniteLoop to set
     */
    public ProductImageAdapter setInfiniteLoop(boolean isInfiniteLoop) {
        this.isInfiniteLoop = isInfiniteLoop;
        return this;
    }
    
    
}
